<?php

namespace App\Http\Controllers\Admin\Collection;

use App\Exceptions\CollectionException;
use App\Models\Collection\CollectionModel;
use App\Services\Collection\CollectionService;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Validation\Rule;

class CollectionController extends BaseCollectionController
{

    private CollectionService $collectionService;

    /**
     * @throws Exception
     */
    public function __construct()
    {
        parent::__construct();
        $this->collectionService = loadService('Collection\CollectionService');
    }

    /**
     * 创建藏品
     *
     * @return JsonResponse
     * @throws Exception
     */
    public function create(): JsonResponse
    {
        $rule = [
            'brandId'      => 'required|integer|min:1',
            'title'        => 'required|string|min:1',
            'type'         => [
                'required', Rule::in([
                    CollectionModel::TYPE_MODEL,
                    CollectionModel::TYPE_SPACE,
                    CollectionModel::TYPE_PICTURE,
                ]),
            ],
            'storyContent' => 'required|string|min:1',
        ];

        // 选择了模型类型，则必须传具体的模型类型subType
        if ($this->params['type'] == CollectionModel::TYPE_MODEL && (empty($this->params['subType']) || !in_array($this->params['subType'], [
                    CollectionModel::SUB_TYPE_OBJ,
                    CollectionModel::SUB_TYPE_GJB,
                ]))) {
            return outputJsonError();
        }

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->collectionService->create($this->params);
        return outputJsonResult();
    }

    /**
     * 更新藏品
     *
     * @return JsonResponse
     * @throws CollectionException
     */
    public function update(): JsonResponse
    {
        $rule = [
            'id'           => 'required|integer|min:1',
            'brandId'      => 'required|integer|min:1',
            'title'        => 'required|string|min:1',
            'type'         => [
                'required', Rule::in([
                    CollectionModel::TYPE_MODEL,
                    CollectionModel::TYPE_SPACE,
                    CollectionModel::TYPE_PICTURE,
                ]),
            ],
            'storyContent' => 'required|string|min:1',
        ];

        // 选择了模型类型，则必须传具体的模型类型subType
        if ($this->params['type'] == CollectionModel::TYPE_MODEL && (empty($this->params['subType']) || !in_array($this->params['subType'], [
                    CollectionModel::SUB_TYPE_OBJ,
                    CollectionModel::SUB_TYPE_GJB,
                ]))) {
            return outputJsonError();
        }

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->collectionService->update($this->params);
        return outputJsonResult();
    }

    /**
     * 删除藏品
     *
     * @return JsonResponse
     * @throws Exception
     */
    public function delete(): JsonResponse
    {
        $rule = [
            'id' => 'required|integer|min:1',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->collectionService->delete($this->params);
        return outputJsonResult();
    }

    /**
     * 批量删除藏品
     *
     * @return JsonResponse
     * @throws Exception
     */
    public function deleteByIds(): JsonResponse
    {
        $rule = [
            'ids' => 'required|array',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->collectionService->deleteByIds($this->params);
        return outputJsonResult();
    }

    /**
     * 更新藏品状态
     *
     * @return JsonResponse
     * @throws Exception
     */
    public function changeStatus(): JsonResponse
    {
        $rule = [
            'id'     => 'required|integer|min:1',
            'status' => 'integer|in:0,1',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->collectionService->changeStatus($this->params);
        return outputJsonResult();
    }

    /**
     * 藏品列表
     *
     * @return JsonResponse
     * @throws Exception
     */
    public function list(): JsonResponse
    {
        $list = $this->collectionService->list($this->params);
        return outputJsonResult($list);
    }

    /**
     * 藏品详情
     *
     * @return JsonResponse
     * @throws CollectionException
     */
    public function detail(): JsonResponse
    {
        $rule = [
            'id' => 'required|integer|min:1',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $detail = $this->collectionService->detail($this->params);
        return outputJsonResult($detail);
    }

}
